package org.bugeden.netdisk.main

import org.apache.commons.cli.{PosixParser, CommandLine, HelpFormatter, Options}
import org.bugeden.netdisk.vfs.MDisk
import org.bugeden.netdisk.transfer.MailBox
import org.bugeden.netdisk.user.User
import java.io.File

/**
 * Created by IntelliJ IDEA.
 * User: inotseeyou
 * Date: 2010-11-13
 * Time: 17:46:08
 * To change this template use File | Settings | File Templates.
 */

object DemoMain {
  val opts = options
  var cmd: CommandLine = null
  var disk: MDisk = new MDisk

  def main(args: Array[String]) {
    this.preMain(args)

    if (cmd.hasOption("c")) {
       this.createFile
    }else if(cmd.hasOption("u")){

    }else if(cmd.hasOption("d")){
      this.downloadFile
    }else if(cmd.hasOption("l")){
      this.list()
    }else{
      this.printHelp
    }
  }

  private def list(){
    val list = disk.list(cmd.getOptionValue("l"))
    list.foreach(println(_))
  }

  private def downloadFile(){

  }

  def preMain(args: Array[String]) {
    val parser = new PosixParser()
    cmd = parser.parse(options, args)
    //-----------------------config-----------------------//
    val mailbox = new MailBox
    mailbox.bind(User())
    disk.setDevice(mailbox)
  }

  private def createFile() {
    if (!cmd.hasOption("f")) {
      println("please input the filename you want to create on mdisk")
    }
    val pathToSave = cmd.getOptionValue("c")
    val lfile = new File(cmd.getOptionValue("f"))
    disk.create(pathToSave, lfile)
  }


  private def options(): Options = {
    val options = new Options
    options.addOption("h", "help", false, "print help information")
    options.addOption("c", "create", true, "the position you want to create file on mdisk")
    options.addOption("f", "local", true, "the local file you want to do something")
    options.addOption("u", "update", true, """update the specify file with the local file specified by "-f" """)
    options.addOption("d", "download", true, "download the spiecify file to  loccal file specified by \"-f\"")
    options.addOption("m", "mkdir", true, "make a directory to mdisk")
    options.addOption("l", "list", true, "print the list file/directory of the specify directory")
    options.addOption("cf", "config file", true, "the configuration file to set the email")
  }

  private def printHelp() {
    val formatter = new HelpFormatter
    formatter.printHelp("netdisk-snapshot", options)
  }
}